<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>后台管理</title>
<!--引入layui的样式-->
<link rel="stylesheet" type="text/css" href="../layui/css/layui.css" />
<!-- 引入layui的脚本文件 -->
<script src="../layui/layui.js" type="text/javascript" charset="UTF-8"></script>
</head>
<body>
	<!-- 表格容器 -->
	<table id="demo" lay-filter="test"></table>
	<!-- 工具栏模板头部 -->
	<script type="text/html" id="toolbarDemo1">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
  </div>
</script>

	<!-- 工具栏模板行内 -->
	<script type="text/html" id="toolbarDemo2">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="update">编辑</button>
    <button class="layui-btn layui-btn-sm" lay-event="delete">删除</button>
  </div>
</script>

	<!-- 添加的表单 -->
	<div id="addDiv" style="display: none">
		<form class="layui-form" sction="">
			<div class="layui-form-item">
				<label class="layui-form-label">标题</label>
				<div class="layui-input-block">
					<input type="text" name="title" required lay-verify="required"
						placeholder="请输入标题" autocomplete="off" class="layui-input">
				</div>
			</div>
			<!-- <div class="layui-form-item">
			<label class="layui-form-label">url</label>
			<div class="layui-input-block">
				<input type="text" name="url" required lay-verify="required"
					placeholder="请输入标题" autocomplete="off" class="layui-input">
			</div>
		</div> -->
			<div class="layui-form-item">
				<label class="layui-form-label">权重</label>
				<div class="layui-input-block">
					<input type="text" name="weight" required lay-verify="required"
						placeholder="请输入标题" autocomplete="off" class="layui-input">
				</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label">url</label>
				<div class="layui-input-block">
					<!-- 上传图片的按钮 -->
					<button type="button" class="layui-btn layui-btn-sm" id="test1">
						<i class="layui-icon">&#xe67c;</i>上传图片
					</button>

					<!-- 展示上传后的图片 -->
					<img id="showImg1" src="" width="50">
					<!-- 用来存储上传之后的图片 -->
					<input type="text" name="url" id="url1" required
						lay-verify="required" placeholder="请输入地址" autocomplete="off"
						class="layui-input layui-disabled">
				</div>
			</div>



			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
				</div>
			</div>
		</form>
	</div>
	<!-- 添加表单结束 -->

	<!-- 修改表单 -->
	<div id="updateDiv" style="display: none">
		<form class="layui-form" sction="" id="updateForm" lay-filter="updateForm">
			<div class="layui-form-item">
				<label class="layui-form-label">id</label>
				<div class="layui-input-inline">
					<input type="text" name="id" required lay-verify="required"
						placeholder="请输入标题" autocomplete="off"
						class="layui-input layui-disabled">
				</div>

			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">标题</label>
				<div class="layui-input-block">
					<input type="text" name="title" required lay-verify="required"
						placeholder="请输入标题" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">权重</label>
				<div class="layui-input-block">
					<input type="text" name="weight" required lay-verify="required"
						placeholder="请输入标题" autocomplete="off" class="layui-input">
				</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label">url</label>
				<div class="layui-input-block">
					<!-- 上传图片的按钮 -->
					<button type="button" class="layui-btn layui-btn-sm" id="test2">
						<i class="layui-icon">&#xe67c;</i>上传图片
					</button>

					<!-- 展示上传后的图片 -->
					<img id="showImg2" src="" width="50">
					<!-- 用来存储上传之后的图片 -->
					<input type="text" name="url" id="url2" required
						lay-verify="required" placeholder="请输入地址" autocomplete="off"
						class="layui-input layui-disabled">
				</div>
			</div>



			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn" lay-submit lay-filter="formDemo2">立即提交</button>
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
				</div>
			</div>
		</form>
	</div>
	<!-- 修改表单结束 -->

	<!-- 脚本开始 -->
	<script>
		layui.use([ 'table', 'form', 'layer', 'upload' ], function() {
			var table = layui.table;
			var form = layui.form;
			var layer = layui.layer;
			var $ = layui.$;
			var upload = layui.upload;

			//第一个实例BannerServletAdmin?method=getList

			table.render({
				elem : '#demo',
				url : 'BannerServletAdmin?method=getList', //数据接口
				page : true,//开启分页
				id : "idTest",
				toolbar : '#toolbarDemo1',
				cols : [ [ //表头
				{
					field : 'id',
					title : 'ID',
					width : 80
				}, {
					field : 'title',
					title : '标题',
					width : 80
				}, {
					field : 'url',
					title : '图片名称',
					width : 80,
					templet : function(d) {
						return '<img width="50" src="../'+d.url+'">'
					}
				}, {
					field : 'weight',
					title : '签名',
					width : 177
				}, {
					title : '操作',
					width : 177,
					toolbar : '#toolbarDemo2',
				}

				] ]
			});//表格渲染结束
			//监听事件 头部工具栏监听
			table.on('toolbar(test)', function(obj) {
				if (obj.event == "add") {
					//layer.msg('添加');
					layer.open({
						type : 1,
						content : $('#addDiv')
					//这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
					});
				}
				;
			});//监听事件 头部工具栏监听结束

			//监听事件  行内
			table.on('tool(test)', function(obj) {
				var data = obj.data;//当前行的数据
				switch (obj.event) {
				case 'delete':
					//layer.msg('删除');
					layer.confirm('真的删除么'+data.id, function(index) {
						obj.del();//删除对应行的结构 更新缓存
						layer.close(index);
						//向服务端发送删除命令BannerServletAdmin?method=delBanner
						$.post("BannerServletAdmin?method=delBanner", {
							"id" : data.id
						}, function(res) {
							layer.msg(res.msg);
						}, 'json')

					});

					break;
				case 'update':
					//layer.msg('编辑');
					//弹窗
					layer.open({
						type : 1,
						content : $('#updateDiv')

					});
					//数据回显
					form.val("updateForm", data);
					$("#showImg2").attr("src", "../" + data.url);
					break;
				}
				;
			});//监听事件 结束

			//监听提交--添加
			form.on('submit(formDemo)', function(data) {
				// layer.msg(JSON.stringify(data.field));
				$.post("BannerServletAdmin?method=insertBanner", data.field,
						function(res) {
							console.log(res);
							if (res.code == 0) {
								layer.msg(res.msg, {
									time : 1000
								}, function() {
									layer.closeAll();//关闭所有窗口
									table.reload('idTest', {});//表格重载 需要id来指明
								})
							}
						}, "json")

				return false;
			});
			
			//监听提交  修改
			form.on('submit(formDemo2)', function(data){
				$.post("BannerServletAdmin?method=updateBanner", data.field,
						function(res) {
					console.log(res);
					if (res.code == 0) {
						layer.msg(res.msg, {
							time : 1000
						}, function() {
							layer.closeAll();//关闭所有窗口
							table.reload('idTest', {});//表格重载 需要id来指明
						})
					}
				}, "json")

		return false;
	});

			//添加图片上传的执行实例ImgServletAdmin?method=uploadImg
			var uploadInst = upload.render({
				elem : '#test1' //绑定元素
				,
				url : 'ImgServletAdmin?method=uploadImg' //上传接口
				,
				done : function(res) {
					//上传完毕回调
					console.log(res);
					if (res.code == 0) {
						//展示
						$("#showImg1").attr("src", "../images/" + res.data);
						//url的值
						$("#url1").val("images/" + res.data);
					}
				},
				error : function() {
					//请求异常回调
				}
			});

			//编辑修改图片上传的执行实例  修改
			var uploadInst = upload.render({
				elem : '#test2' //绑定元素
				,
				url : 'ImgServletAdmin?method=uploadImg' //上传接口
				,
				done : function(res) {
					//上传完毕回调
					console.log(res);
					if (res.code == 0) {
						//展示
						$("#showImg2").attr("src", "../images/" + res.data);
						//url的值
						$("#url2").val("images/" + res.data);
					}
				},
				error : function() {
					//请求异常回调
				}
			});

		});
	</script>
	<!-- 脚本结束 -->
</body>
</html>